﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sklad1_2.Default;

namespace Sklad1_2.Add.Update
{
    public partial class UpdateMain : Defaultform
    {
        private double _temp;
        private double _price;
        private readonly double _use1;
        private bool _flag;
        private bool _flag1;
        private List<string> k; 
        public UpdateMain()
        {
            InitializeComponent();
        }

        public UpdateMain(List<string> item)
            : base(item)
        {
            InitializeComponent();
            k = item;
            //foreach (var a in item)
            //{
            //    MessageBox.Show(a);
            //}
            
            ABase.AddItem(comboBox1, "customer");
            comboBox1.SelectedValue = item[1];
            ABase.AddItem(comboBox2, "angar");
            comboBox2.SelectedValue = item[2];
            ABase.AddItem(comboBox3, "category");
            comboBox3.SelectedValue = item[3];
            ABase.AddItemFirma(comboBox4, "Firma");
            comboBox4.SelectedValue = item[4];
            ABase.AddItemProduct(comboBox5, "product");
            comboBox5.SelectedValue = item[5];
            checkBox1.Checked = Boolean.Parse(item[8]);
            textBox1.Text = item[6];
            textBox5.Text = item[7];
            _use1 = Double.Parse(textBox1.Text);
            textBox3.Text = item[9];
            dateTimePicker1.Text = item[10];
            textBox7.Text =item[11] ;
            textBox8.Text = item[12];
            if (textBox3.Text!=textBox8.Text)
            {
                dateTimePicker1.Enabled=comboBox5.Enabled = comboBox4.Enabled = comboBox3.Enabled = comboBox2.Enabled = comboBox1.Enabled = checkBox2.Enabled = checkBox1.Enabled = textBox4.Enabled = textBox1.Enabled = button3.Enabled = false;
                
            }
            if (checkBox1.Checked)
            {
                checkBox2.Checked = true;
                checkBox2.Enabled = false;
            }
        }

        #region Button

        private void Button2Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region RadioButton


        #endregion

        #region TextBox

        private void TextBox1TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                if (_flag1)
                {
                    var t1 = Double.Parse(textBox1.Text);
                    textBox5.Text = Convert.ToString(t1*_temp);
                }
                else
                {
                    var t1 = Double.Parse(textBox1.Text);
                    textBox5.Text = Convert.ToString(t1);
                }
            }
            else
            {
                textBox1.Text = "0";
            }
        }

        private void TextBox4TextChanged(object sender, EventArgs e)
        {
            if (textBox4.Text.Length > 0)
            {
                var t1 = Double.Parse(textBox4.Text);
                if (_flag)
                {
                    var res = t1 * _temp * _price;
                    textBox2.Text = Convert.ToString(res);
                    textBox6.Text = Convert.ToString(t1*_temp);
                }
                else
                {
                    var res = t1 * _price;
                    textBox2.Text = Convert.ToString(res);
                    textBox6.Text = Convert.ToString(t1);
                }
            }
            else
            {
                textBox4.Text = "0";
            }
        }

        private void TextBox3TextChanged(object sender, EventArgs e)
        {
            textBox8.Text = textBox3.Text;
        }


        private void TextBox4KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }

        #endregion

        #region ComboBox

        private void ComboBox2SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox2.SelectedValue != null)
            {
                if (comboBox2.SelectedValue.GetType() == t)
                {
                    ABase.AddItemCategory(comboBox3, Convert.ToInt32(comboBox2.SelectedValue));
                    if (comboBox3.SelectedValue == null)
                    {
                        comboBox4.DataSource = null;
                    }
                }
            }
            else
            {
                comboBox3.DataSource = null;
            }
        }

        private void ComboBox3SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox3.SelectedValue != null)
            {
                if (comboBox3.SelectedValue.GetType() == t)
                {
                    ABase.AddItemFirma(comboBox4, Convert.ToInt32(comboBox3.SelectedValue));
                    if (comboBox4.SelectedValue == null)
                    {
                        comboBox5.DataSource = null;
                    }
                }
            }
            else
            {
                comboBox4.DataSource = null;
            }
        }

        private void ComboBox4SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox4.SelectedValue != null)
            {
                if (comboBox4.SelectedValue.GetType() == t)
                {
                    ABase.AddItemProduct(comboBox5, Convert.ToInt32(comboBox4.SelectedValue));
                }
            }
            else
            {
                comboBox5.DataSource = null;
            }
        }

        private void ComboBox5SelectedValueChanged(object sender, EventArgs e)
        {
            Type t = typeof(Int32);
            if (comboBox5.SelectedValue != null)
            {
                if (comboBox5.SelectedValue.GetType() == t)
                {
                    int id1 = Convert.ToInt32(comboBox5.SelectedValue);
                    _temp =
                    Int32.Parse(ABase.SelectField("product", "count1", "id", id1));
                    _price =
                        Double.Parse(ABase.SelectField("product", "price", "id", id1));
                }
            }
        }
        #endregion

        #region Button
        private void Button3Click(object sender, EventArgs e)
        {
            var t2 = Math.Round(Double.Parse(textBox5.Text),0);
            double res = 0;
            if (textBox4.Text.Length > 0)
            {
                var t1 = Double.Parse(textBox6.Text);
                if (t2 >= t1)
                {
                    res = t2 - t1;
                }
                else
                {
                    MessageBox.Show("Вы возвращяете в большем кол-во!!!");
                    textBox4.Text = "0";
                    return;
                }
            }
            else
            {
                textBox4.Text = "0";
            }
            textBox5.Text = Convert.ToString(Math.Round(res,0));
            ABase.UpdateValueMain(textBox1.Text,textBox3.Text,textBox8.Text,id);
            ABase.InsertValueVozvrat(id,textBox4.Text,textBox2.Text,DateTime.Today.ToShortDateString(),dateTimePicker1.Text);
        }



        private void Button4Click(object sender, EventArgs e)
        {
            var temp1 = Double.Parse(textBox8.Text);
            if (textBox7.Text.Length > 0)
            {
                var q = Double.Parse(textBox7.Text);
                if (temp1 >= q)
                {
                    var w = temp1 - q;
                    textBox8.Text = Convert.ToString(w);
                    ABase.UpdateValueMain(Convert.ToString(q), Convert.ToString(temp1), id);
                    ABase.InsertValueRas(id, Convert.ToString(q), Convert.ToString(temp1), DateTime.Today.ToShortDateString());
                    if (textBox3.Text != textBox8.Text)
                    {
                        dateTimePicker1.Enabled = comboBox5.Enabled = comboBox4.Enabled = comboBox3.Enabled = comboBox2.Enabled = comboBox1.Enabled = checkBox2.Enabled = checkBox1.Enabled = textBox4.Enabled = textBox1.Enabled = button3.Enabled = false;

                    }
                }
                else
                {
                    MessageBox.Show("Значение возврата превыщает значение покупки!!!");
                    textBox7.Text = "0";
                }
            }
            else
            {
                textBox7.Text = "0";
            }
        }

        #endregion


        #region CheckBox
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            _flag1 = checkBox1.Checked;
            checkBox2.Checked = _flag1;
            checkBox2.Enabled = false;
            TextBox1TextChanged(null, null);
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            _flag = checkBox2.Checked;
            TextBox4TextChanged(null,null);
        }
        #endregion

        private void ButtonClick(object sender, EventArgs e)
        {
            for (int i=0;i<textBox1.Text.Length;i++)
            {
                if (textBox1.Text[i] == ',')
                {
                    textBox1.Text.Replace(',', '.');
                }
            }
            var t1 = Double.Parse(textBox1.Text);
            ABase.UpdateValueMain(Convert.ToString(comboBox1.SelectedValue), Convert.ToString(comboBox2.SelectedValue), Convert.ToString(comboBox3.SelectedValue), Convert.ToString(comboBox4.SelectedValue), Convert.ToString(comboBox5.SelectedValue), Convert.ToString(t1), Convert.ToString(Double.Parse(textBox5.Text)), Convert.ToString(checkBox1.Checked), Convert.ToString(Double.Parse(textBox3.Text)), dateTimePicker1.Text, Convert.ToString(Double.Parse(textBox7.Text)), Convert.ToString(Double.Parse(textBox8.Text)), DateTime.Today.ToShortDateString(), id);
            Close();
        }

        private void TextBox5TextChanged(object sender, EventArgs e)
        {
            if (_flag1)
            {
                textBox1.Text = Convert.ToString(Math.Round((Convert.ToDouble(textBox5.Text) / _temp), 1));
                var res = Double.Parse(textBox5.Text)*_price;
                textBox3.Text = Convert.ToString(Math.Round(res,1));
            }
            else
            {
                textBox1.Text = textBox5.Text;
                var res = Double.Parse(textBox5.Text) * _price;
                textBox3.Text = Convert.ToString(res);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }










    }
}
 
